﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace ActivityTracker.Library
{
    public class DailyChart
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isoStream;
        XDocument xdoc;

        public List<ChartProp> LoadProgressData(string actId)
        {
            List<ChartProp> progressChartList = new List<ChartProp>();
            try
            {
                if (isoStore.FileExists("ActivityTracker/TotalDurations.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/TotalDurations.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activitylist = from t in xdoc.Descendants("dailyduration")
                                       select t;
                    foreach (var act in activitylist.Descendants("day"))
                    {
                        var elements = from ac in act.Descendants("activity") where ac.Attribute("actId").Value == actId select ac;
                        foreach (XElement ele in elements)
                        {
                            ChartProp pd = new ChartProp();
                            pd.ActDate = Convert.ToDateTime(act.Attribute("date").Value).ToShortDateString();
                            pd.ActDur = Convert.ToDouble(Convert.ToInt32(ele.Attribute("totDur").Value) / Convert.ToInt32(ele.Attribute("count").Value));
                            progressChartList.Add(pd);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadProgressData Method In ActivityTracker.Library.Charts.Charts file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exnew.Data.Add("actId_LoadProgressData", actId);
                throw exnew;
            }
            return progressChartList;
        }

        public List<ChartProp> LoadTimeSpentData(DateTime dt, ChartType Type)
        {
            List<ChartProp> timeSpentChartList = new List<ChartProp>();
            try
            {
                if (isoStore.FileExists("ActivityTracker/TotalDurations.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/TotalDurations.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activitylist = (from t in xdoc.Descendants("day")
                                        where t.Attribute("date").Value == dt.ToShortDateString()
                                        select t);
                    double othersDuration = 0;
                    if (Type == ChartType.Pie)
                    {
                        double totDur = 0;
                        totDur = (from list in activitylist.Descendants("activity")
                                  select (Convert.ToInt32(list.Attribute("totDur").Value))).Sum();
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants("activity"))
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            if ((cp.ActDur * 100 / totDur) <= 5)
                                othersDuration += cp.ActDur;
                            else
                            {
                                cp.ActName = activ.Attribute("actName").Value;
                                timeSpentChartList.Add(cp);
                            }
                        }
                        othercp.ActName = "Other Activities";
                        othercp.ActDur = othersDuration;
                        if (othersDuration > 0)
                            timeSpentChartList.Add(othercp);
                    }
                    if (Type == ChartType.Bar)
                    {
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants("activity"))
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            cp.ActName = activ.Attribute("actName").Value;
                            if(cp.ActDur!=0)
                            timeSpentChartList.Add(cp);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadTimeSpentData Method In ActivityTracker.Library.Charts.Charts file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }
            return timeSpentChartList;
        }

        public List<ChartProp> LoadCategoryTimeSpentData(DateTime dt, ChartType Type)
        {
            List<ChartProp> timeSpentChartList = new List<ChartProp>();
            try
            {
                if (isoStore.FileExists("ActivityTracker/CategoryTotalDurations.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/CategoryTotalDurations.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activitylist = (from t in xdoc.Descendants("day")
                                        where t.Attribute("date").Value == dt.ToShortDateString()
                                        select t);
                    double othersDuration = 0;
                    if (Type == ChartType.Pie)
                    {
                        double totDur = 0;
                        totDur = (from list in activitylist.Descendants("category")
                                  select (Convert.ToInt32(list.Attribute("totDur").Value))).Sum();
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants("category"))
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            if ((cp.ActDur * 100 / totDur) <= 5)
                                othersDuration += cp.ActDur;
                            else
                            {
                                cp.CatName = activ.Attribute("catname").Value;
                                timeSpentChartList.Add(cp);
                            }
                        }
                        othercp.CatName = "Other Activities";
                        othercp.ActDur = othersDuration;
                        if (othersDuration > 0)
                            timeSpentChartList.Add(othercp);
                    }
                    if (Type == ChartType.Bar)
                    {
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants("category"))
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            cp.CatName = activ.Attribute("catname").Value;
                            if (cp.ActDur != 0)
                                timeSpentChartList.Add(cp);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadTimeSpentData Method In ActivityTracker.Library.Charts.Charts file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }
            return timeSpentChartList;
        }
    }
}
